package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.Article;
import com.briup.jz.bean.ArticleExample;
import com.briup.jz.dao.ArticleMapper;
import com.briup.jz.dao.extend.ArticleExtendMapper;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.service.IArticleService;
import com.briup.jz.utils.CustomerException;
@Service
public class ArticleServiceImpl implements IArticleService{
	
	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private ArticleExtendMapper articleExtendMapper;
	
	@Override
	public void saveOrUpdate(Article article)  {
		if(article.getId()!=null) {
	/*		articleMapper.updateByPrimaryKey(article);}
		
		 else {
			// 判断是否有同名，如果有抛出异常
			ArticleExample example = new ArticleExample();
			example.createCriteria().andIdEqualTo(article.getId());
			List<Article> list = articleMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名已存在");
			}  */
   	    article.setPublishTime(new Date().getTime());
		    article.setReadTimes(0l);
		    article.setThumpUp(0l);
		    article.setStatus("未审核");
			articleMapper.insert(article);
		}else {     
			
			articleMapper.updateByPrimaryKey(article);
		}

}

	public List<Article> query(String title,String status,Long categoryId) {
		
		ArticleExample example = new ArticleExample();
		
		 ArticleExample.Criteria criteria = example.createCriteria();
		 if(title !=null) {
			 
			 criteria.andTitleLike("%"+title+"%");
		 }
         if(status !=null) {
			 
			 criteria.andStatusEqualTo(status);
		 }
		 if(categoryId !=null) {
			 criteria.andCategoryIdEqualTo(categoryId);
		 }
		
		return articleMapper.selectByExample(example);
	}
/*		ArticleExample example=new ArticleExample();
		if(id!=null) {
			example.createCriteria().andIdEqualTo(id);
		}
		List<Article> list = articleMapper.selectByExample(example);
		return list;
*/
	
		
	@Override
	public void deleteById(long id) throws CustomerException {
		Article article=articleMapper.selectByPrimaryKey(id);
		if(article==null) {
			throw new CustomerException("要删除不存在");
		}
		articleMapper.deleteByPrimaryKey(id);		
	}

	@Override
	public void cascadeFindAll() throws CustomerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void changeStatus(String id, String status) throws CustomerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void download() throws CustomerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void findAllRecommend(String page, String pageSize) throws CustomerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void findById(String id) throws CustomerException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pageQuery(String page, String pageSize, String title, String status, String authorId, String categoryId)
			throws CustomerException {
		// TODO Auto-generated method stub
	}

	public List<ArticleExtend> queryCascade(String title, String status, Long categoryId) {
		// TODO Auto-generated method stub
		return articleExtendMapper.select(title, status, categoryId);
	}

	@Override
	public ArticleExtend read(long id) {
		
		return ArticleExtendMapper.selectById(id);
	}	
	
}

